package com.ap.core.dao.impl;

import javax.persistence.EntityManager;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.LocalDateTime;

import com.ap.core.common.types.MessageStatus;
import com.ap.core.dao.AbstractAPCoreBaseJpaDao;
import com.ap.core.dao.OrderPaymentDao;
import com.ap.core.domain.OrderPayment;
import com.ap.core.domain.PaymentConfirmationUpdateObj;

/**
 * User: Ranjit.Singh
 * Jpa Implementation class for {@link com.ap.core.dao.OrderPaymentDao}
 */
public class JpaOrderPaymentDao extends AbstractAPCoreBaseJpaDao<OrderPayment, String> implements OrderPaymentDao{

    @Override
    public OrderPayment findById(String primaryKey) {
        return findById(OrderPayment.class, primaryKey);
    }

    @Override
    public EntityManager getEntityManager() {
        return entityManager;
    }

    @Override
	public OrderPayment getReadOnlyOrderPaymentByApTranId(String apTranId) {
		final Criteria criteria = getSession().createCriteria(OrderPayment.class);
        criteria.add(Restrictions.eq("id", apTranId));
        criteria.setReadOnly(true);
        return (OrderPayment)criteria.uniqueResult();
	}

    @Override
    public void updateOrderPayment(String id, MessageStatus messageStatus, Long zappConsumerId, Long fiId,Long apMessageId, String fiCustomerId, LocalDateTime confirmationExpiryTime) {
        javax.persistence.Query query = entityManager.createNamedQuery("updateOrderPayment");
        query.setParameter("messageStatus", messageStatus);
        query.setParameter("zappConsumerId", zappConsumerId);
        query.setParameter("fiId", fiId);
        query.setParameter("statusTimeStmp", new LocalDateTime());
        query.setParameter("lastUpdateTimestmp", new LocalDateTime());
        query.setParameter("confirmationExpiryTimeStmp", confirmationExpiryTime);
        query.setParameter("apMessageId", apMessageId);
        query.setParameter("id", id);
        query.setParameter("fiCustomerId", fiCustomerId);
        query.executeUpdate();

    }

    
    @Override
    public void updateOrderPayment(String id, MessageStatus messageStatus, LocalDateTime confirmationExpiryTime) {
        javax.persistence.Query query = entityManager.createNamedQuery("updateOrderPaymentMessageStatus");
        query.setParameter("id", id);
        query.setParameter("messageStatus", messageStatus);
        query.setParameter("statusTimeStmp", new LocalDateTime());
        query.setParameter("lastUpdateTimestmp", new LocalDateTime());      
        query.setParameter("confirmationExpiryTimeStmp", confirmationExpiryTime);
        query.executeUpdate();
    }
    
	@Override
	public void updateOrderPaymentOnConfirmation(String id,
			MessageStatus messageStatus,
			PaymentConfirmationUpdateObj paymentConfObj) {
		javax.persistence.Query query = entityManager.createNamedQuery("updateOrderPaymentOnConfirmation");
        query.setParameter("messageStatus", messageStatus);
        query.setParameter("transactionStatus", paymentConfObj.getTransactionStatus());
        query.setParameter("transactionStatusCode", paymentConfObj.getTransactionStatusCode().value());
        query.setParameter("paymentDescTimeStamp", paymentConfObj.getPaymentDescTimeStamp());
        query.setParameter("fasterPaymentRef", paymentConfObj.getFasterPaymentRef());
        query.setParameter("debitAccountNumber", paymentConfObj.getAccountNumber());
        query.setParameter("debitSortCode", paymentConfObj.getSortCode());
        query.setParameter("id", id);
        query.setParameter("statusTimeStmp", new LocalDateTime());
        query.setParameter("lastUpdateTimestmp", new LocalDateTime());
        query.setParameter("confirmationTimeStmp", new LocalDateTime());
        query.setParameter("rollNumber",paymentConfObj.getRollNumber());
        query.setParameter("apMessageId", paymentConfObj.getApMessageId());
        query.executeUpdate();
		
	}
    
    
  

}
